package com.tbit.main.controller;

import com.alibaba.fastjson.JSON;
import com.tbit.main.common.JsonResult;
import com.tbit.main.controller.interceptor.LoginUserContextHolder;
import com.tbit.main.pojo.AccountUser;
import com.tbit.manager.api.NoticeApi;
import com.tbit.manager.api.dto.NoticeDTO;
import com.tbit.manager.api.dto.NoticeSearchDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private NoticeApi noticeApi;


    /**
     * @description: 获取当前用户的公告列表
     * @author: mo.shanyong
     * @date: 2025/2/19 17:12
     * @return: com.tbit.uqbike.object.view.JsonResult
     */
    @GetMapping("/getUserNoticeList")
    public JsonResult getUserNoticeList(Integer pageNum, Integer pageSize, HttpServletRequest request){
//        pageSize = pageSize > 1000 ? 100 : pageSize;
        // 获取用户信息
        AccountUser loginUser = LoginUserContextHolder.getLoginUser();
        NoticeSearchDTO searchDTO = new NoticeSearchDTO();
        searchDTO.setUserId(loginUser.getAccountUserId());
        searchDTO.setPhone(loginUser.getPhone());
        searchDTO.setBrandId(loginUser.getBrandId());
        searchDTO.setAccountId(loginUser.getAccountId());
        searchDTO.setAccountUserType(loginUser.getAccountUserType());
        searchDTO.setPageNum(pageNum);
        searchDTO.setPageSize(pageSize);
        Map<String, Object> userNoticeMap = noticeApi.getUserNoticeList(JSON.toJSONString(searchDTO));
        JsonResult<Object> succ = JsonResult.succ((Integer) userNoticeMap.get("total"), userNoticeMap.get("data"));
        succ.setNum((Integer) userNoticeMap.get("num"));
        return succ;
    }

    /**
     * @description: 记录对公告的操作日志
     * @author: mo.shanyong
     * @date: 2025/2/20 15:48
     * @param: operateType 操作类型：3、点击；5、阅读
     * @return: com.tbit.uqbike.object.view.JsonResult
     */
    @PostMapping("/recordNoticeLog")
    public JsonResult recordNoticeLog(Integer noticeId ,Integer operateType){
        log.info("recordNoticeLog------->noticeId:{},operateType:{}",noticeId,operateType);
        // 获取用户信息
        AccountUser loginUser = LoginUserContextHolder.getLoginUser();
        NoticeDTO noticeDTO = new NoticeDTO(noticeId,operateType, loginUser.getAccountUserId());
        Integer logId = noticeApi.recordNoticeLog(noticeDTO);
        return JsonResult.succ(logId);
    }
}
